import { TimeRules } from './timeRule';
let _TimeRules = TimeRules;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import TimeRuleMock from './mk/mng/card/timeRuleMock';
 * TimeRuleMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mk/mng/card/timeRule/listKey').reply(config => {
      let params = config.params;
      let mockTimeRules = _TimeRules.filter(timeRule => {
      	if(params.type||params.type==''){ if( timeRule.type.indexOf(params.type)>=0) return true;}
      	if(params.beginHour||params.beginHour==''){ if( timeRule.beginHour.indexOf(params.beginHour)>=0) return true;}
      	if(params.beginMinute||params.beginMinute==''){ if( timeRule.beginMinute.indexOf(params.beginMinute)>=0) return true;}
      	if(params.endHour||params.endHour==''){ if( timeRule.endHour.indexOf(params.endHour)>=0) return true;}
      	if(params.endMinute||params.endMinute==''){ if( timeRule.endMinute.indexOf(params.endMinute)>=0) return true;}
      	if(params.id||params.id==''){ if( timeRule.id.indexOf(params.id)>=0) return true;}
      	if(params.cardPkId||params.cardPkId==''){ if( timeRule.cardPkId.indexOf(params.cardPkId)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockTimeRules
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mk/mng/card/timeRule/list').reply(config => {
      let params = config.params;
      let mockTimeRules = _TimeRules.filter(timeRule => {
      	var isTrue=false;
      	if(params.type||params.type==''){if(timeRule.type.indexOf(params.type)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.beginHour||params.beginHour==''){if(timeRule.beginHour.indexOf(params.beginHour)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.beginMinute||params.beginMinute==''){if(timeRule.beginMinute.indexOf(params.beginMinute)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.endHour||params.endHour==''){if(timeRule.endHour.indexOf(params.endHour)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.endMinute||params.endMinute==''){if(timeRule.endMinute.indexOf(params.endMinute)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.id||params.id==''){if(timeRule.id.indexOf(params.id)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.cardPkId||params.cardPkId==''){if(timeRule.cardPkId.indexOf(params.cardPkId)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockTimeRules
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockTimeRules.length;
      mockTimeRules = mockTimeRules.filter((timeRule, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockTimeRules,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mk/mng/card/timeRule/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  id  } = params;
      _TimeRules = _TimeRules.filter(timeRule => timeRule.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mk/mng/card/timeRule/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _TimeRules = _TimeRules.filter(timeRule => !params.includes(timeRule.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mk/mng/card/timeRule/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { type,beginHour,beginMinute,endHour,endMinute,id,cardPkId } = params;
      _TimeRules.some(timeRule => {
        if (timeRule.id === id) {
			timeRule.type = type;
			timeRule.beginHour = beginHour;
			timeRule.beginMinute = beginMinute;
			timeRule.endHour = endHour;
			timeRule.endMinute = endMinute;
			timeRule.id = id;
			timeRule.cardPkId = cardPkId
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mk/mng/card/timeRule/add').reply(config => {
      var params = JSON.parse( config.data );
      let { type,beginHour,beginMinute,endHour,endMinute,id,cardPkId } = params;
      _TimeRules.push({
		type:type,
		beginHour:beginHour,
		beginMinute:beginMinute,
		endHour:endHour,
		endMinute:endMinute,
		id:id,
		cardPkId:cardPkId
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};